<template>
	<view class="">
		<!-- <view v-if="loading">
			<loading></loading>
		</view> -->
		<view class="container">
			<textarea class="textarea" placeholder="请输入课程名称"  v-model="FormData.knowledgeName"  id="" cols="30" rows="10"></textarea>
			<view class="uploadView">
				<fileUpload :filevalue="filevalue"  ref="getVideo" v-model="FormData.videoUrl" :limit="1" fileType='video' title="点击上传视频"></fileUpload>
			</view>
			<view class="uploadView">
				<imageUpload :imgvalue="imgvalue"  ref="getImg" v-model="FormData.imgUrl" :limit="1"></imageUpload>
			</view>
			<view class="listView">
				<uni-data-picker class="picker" placeholder="归类于" :map="map" popup-title="请选择所在栏目" :localdata="range" v-model="FormData.categoryId">
				</uni-data-picker>
<!-- 				@change="onchange" @nodeclick="onnodeclick" @popupopened="onpopupopened" @popupclosed="onpopupclosed"-->				
<!-- <tki-tree ref="tkitree" title="选择门店" :range="range" rangeKey="title" idKey="key" :selectParent="true"
							:foldAll="true" confirmColor="#4e8af7" @confirm="treeConfirm" @cancel="treeCancel" /> -->
			</view>
			<view class="listView">
				<uni-data-picker class="picker" placeholder="谁可以看"  popup-title="谁可以看" :localdata="range2" v-model="FormData.isPulice">
				</uni-data-picker>
			</view>
			<view class="submit-view">
				<button class='submit'  @tap="clickBtn">提交</button>
			</view>
		</view>
	</view>
	
	
</template>
<script>
	import {getCourse,updateCourse,getSimpleCate,createCourse} from '@/api/exam/activity.js'
	import loading from '@/components/loading/loading'
	import imageUpload from "@/components/imageUpload/imageUpload.vue";
	import fileUpload from "@/components/fileUpload/fileUpload.vue";
	export default {
		components: {
					imageUpload,
					fileUpload
				},
		data() {
			return {
				FormData: {
					isPay:0
				},
				id:null,
				imgvalue:[],
				filevalue:[],
				loading:true,
				range:[],
				range2:[
					{
						text:'公开',
						value:1
					},{
					text:'私有',
					value:0
				}
				],
				map:{
					text:'categoryName',
					value:'id'
				}
			}
		},
		onLoad(e) {
			if(e.id != null && e.id != ''){
				this.id = e.id
				this.getData();
			}
			this.getcate()
			
			if(e.categoryId != null && e.categoryId != ''){
				this.FormData.categoryId = parseInt(e.categoryId)
			}
			
		},
		methods: {
			getData(){
				let data={
					id:this.id
				}
				getCourse(data).then(res=>{
					this.FormData = res.data
					if(res.data.imgUrl != null ){
						this.imgvalue.push({url:res.data.imgUrl})
					}
					if(res.data.videoUrl != null ){
						this.filevalue.push({url:res.data.videoUrl})
					}
					this.loading = false
				})
			},
			getcate(){
				getSimpleCate().then(res=>{
					this.range = this.handleTree(res.data,'id');
				})
			},
			clickBtn(){
				let imgvalue = this.$refs.getImg.getImgValue();
				let filevalue = this.$refs.getVideo.getValue();
				if(imgvalue.length >0){
					this.FormData.imgUrl  = imgvalue[0].url
				}
				if(filevalue.length >0){
					this.FormData.videoUrl = filevalue[0].url
				}
				if(this.FormData.id == null) {
				  this.create()
				}else{
					this.update()
				}
			},
			create(){
					createCourse(this.FormData).then(res=>{
						this.$modal.msgSuccess("新增成功")
						uni.navigateBack({
							delta:1
						})
					})
				
				
			},
			update(){
				updateCourse(this.FormData).then(res=>{
					if(res.data === true){
						this.$modal.msgSuccess("修改成功")
						uni.navigateBack({
							delta:1
						})
					}
				})
			},
			getImgUrl(data){
				console.log(data)
			}
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		min-height: 100%;
		height: auto;
		width: calc(100vw - 60rpx);
		margin: 0 auto;
	}
	
	view {
		font-size: 14px;
		line-height: inherit;
	}
	
	/* #endif */
 .container{
	 margin-top: 40rpx;
	 
	 .textarea{
		 width: 100%;
		 border: #508CFF 2rpx solid;
		 border-radius: 20rpx;
		 padding: 10px;
		 background-color: #fff;
		 font-size: 14px;
	 }
	 .uploadView{
		  margin: 50rpx 0;
			.videoView{
				margin: 20px 0;
			}
		 /deep/ .is-add{
			 display: block;
			 padding-top: 60rpx;
			 uni-text{
			 				 display: block;
			 				 text-align: center;
			 				 line-height: 40rpx;
			 }
		 }
		/deep/ .file-picker__box{
			 width: 200rpx !important;
			 height: 200rpx !important;
			 background: #F0EFEF;
			 color: #969696;
			 border-radius: 20rpx;
		 }
	}
	.listView{
		margin-top: 20rpx;
		.picker{
			background-color: #fff;
		}
		/deep/ .input-value-border{
			border: none;
			box-shadow: none;
			.placeholder{
				font-size: 14px;
				color: #3b4144;
				padding-left: 5px;
			}
		}
	}
	.submit{
		margin-top: 40px;
		color: #fff;
		width: 100px;
		height: 30px;
		font-size: 14px;
		line-height: 30px;
		background: #508CFF;
		border-radius: 5px 5px 5px 5px;
	}
		 
 }
</style>